]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
Util.killall: add utility for killing unwanted processes
authorDan Winship <danw@gnome.org>
Fri, 17 Dec 2010 20:30:12 +0000 (15:30 -0500)
committerDan Winship <danw@gnome.org>
Thu, 13 Jan 2011 17:14:40 +0000 (12:14 -0500)
Since we have to use pkill, kludgily, for the right combination of
portability and featurefulness, put the code in one place rather than
duplicating it everywhere.

https://bugzilla.gnome.org/show_bug.cgi?id=635089

js/misc/util.js
js/ui/notificationDaemon.js
js/ui/status/power.js
js/ui/statusIconDispatcher.js

index e61f0dd94fd770c4c444607c5ba5a7751641fce3..db5dc01c6fedfffea7fb29fe0a7fa7c889bf0c85 100644 (file)
@@ -148,3 +148,28 @@ function _handleSpawnError(command, err) {
     notification.setTransient(true);
     source.notify(notification);
 }
+
+// killall:
+// @processName: a process name
+//
+// Kills @processName. If no process with the given name is found,
+// this will fail silently.
+function killall(processName) {
+    try {
+        // pkill is more portable than killall, but on Linux at least
+        // it won't match if you pass more than 15 characters of the
+        // process name... However, if you use the '-f' flag to match
+        // the entire command line, it will work, but we have to be
+        // careful in that case that we can match
+        // '/usr/bin/processName' but not 'gedit processName.c' or
+        // whatever...
+
+        let argv = ['pkill', '-f', '^([^ ]*/)?' + processName + '($| )'];
+        GLib.spawn_sync(null, argv, null, GLib.SpawnFlags.SEARCH_PATH, null, null);
+        // It might be useful to return success/failure, but we'd need
+        // a wrapper around WIFEXITED and WEXITSTATUS. Since none of
+        // the current callers care, we don't bother.
+    } catch (e) {
+        logError(e, 'Failed to kill ' + processName);
+    }
+}
index b87b3a41d6e33261d0a2d51fd86f45577954ef67..0b377a834a5e54dfa7d4a99f3930497412cefe55 100644 (file)
@@ -13,6 +13,7 @@ const Config = imports.misc.config;
 const Main = imports.ui.main;
 const MessageTray = imports.ui.messageTray;
 const Params = imports.misc.params;
+const Util = imports.misc.util;
 
 let nextNotificationId = 1;
 
@@ -127,17 +128,8 @@ NotificationDaemon.prototype = {
             log('Failed to acquire org.freedesktop.Notifications');
         else {
             log('Failed to acquire org.freedesktop.Notifications; trying again');
-
-            // kill the notification-daemon. pkill is more portable
-            // than killall, but on Linux at least it won't match if
-            // you pass more than 15 characters of the process name...
-            // However, if you use the '-f' flag to match the entire
-            // command line, it will work, but we have to be careful
-            // in that case that we don't match 'gedit
-            // notification-daemon.c' or whatever...
-            let p = new Shell.Process({ args: ['pkill', '-f',
-                                               '^([^ ]*/)?(notification-daemon|notify-osd)$']});
-            p.run();
+            Util.killall('notification-daemon');
+            Util.killall('notify-osd');
         }
     },
 
index fe787f790d29f8e3ca966c5b50d4e8ca9ec667dd..263d48e3daa37576359c3e991f3cecaee16e1daa 100644 (file)
@@ -1,7 +1,6 @@
 /* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
 
 const Gio = imports.gi.Gio;
-const GLib = imports.gi.GLib;
 const DBus = imports.dbus;
 const Lang = imports.lang;
 const Mainloop = imports.mainloop;
@@ -196,7 +195,7 @@ Indicator.prototype = {
 
     _checkError: function(error) {
         if (!this._restarted && error && error.message.match(/org\.freedesktop\.DBus\.Error\.(UnknownMethod|InvalidArgs)/)) {
-            GLib.spawn_command_line_sync('pkill -f "^gnome-power-manager$"');
+            Util.killall('gnome-power-manager');
             Util.spawn(['gnome-power-manager']);
             this._restarted = true;
         }
index 474cf54d3c6b580928af65c0cf5e51ceeebb8055..874ffb40d45fba3dc7e90425297b72a3c67a2ebb 100644 (file)
@@ -6,6 +6,7 @@ const Signals = imports.signals;
 
 const MessageTray = imports.ui.messageTray;
 const NotificationDaemon = imports.ui.notificationDaemon;
+const Util = imports.misc.util;
 
 const STANDARD_TRAY_ICON_IMPLEMENTATIONS = {
     'bluetooth-applet': 'bluetooth',
@@ -33,8 +34,7 @@ StatusIconDispatcher.prototype = {
         // app-indicators, so that applications fall back to normal
         // status icons
         // http://bugzilla.gnome.org/show_bug.cgi=id=621382
-        let p = new Shell.Process({ args: ['pkill', '-f', '^([^ ]*/)?indicator-application-service$']});
-        p.run();
+        Util.killall('indicator-application-service');
     },
 
     _onTrayIconAdded: function(o, icon) {